home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / ODUtils / Sources / StorUtil.cpp < prev   
Encoding:
C/C++ Source or Header  |  1996-04-25  |  12.0 KB  |  436 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        StorUtil.cpp
  3.  
  4.     Contains:    Utilities for working with ODStorageUnits.
  5.  
  6.     Owned by:    Vincent Lo
  7.  
  8.     Copyright:    © 1994 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     
  11.     In Progress:
  12.         
  13. */
  14.  
  15. #ifndef _STORUTIL_
  16. #include <StorUtil.h>
  17. #endif
  18.  
  19. #ifndef SOM_ODDraft_xh
  20. #include <Draft.xh>
  21. #endif
  22.  
  23. #ifndef SOM_ODSession_xh
  24. #include <ODSessn.xh>
  25. #endif
  26.  
  27. #ifndef SOM_ODStorageSystem_xh
  28. #include <ODStor.xh>
  29. #endif
  30.  
  31. #ifndef SOM_ODDocument_xh
  32. #include <Document.xh>
  33. #endif
  34.  
  35. #ifndef SOM_ODContainer_xh
  36. #include <ODCtr.xh>
  37. #endif
  38.  
  39. #ifndef SOM_ODStorageUnit_xh
  40. #include <StorageU.xh>
  41. #endif
  42.  
  43. #ifndef SOM_ODStorageUnitView_xh
  44. #include <SUView.xh>
  45. #endif
  46.  
  47. #ifndef SOM_Module_OpenDoc_StdDefs_defined
  48. #include <StdDefs.xh>
  49. #endif
  50.  
  51. #ifndef SOM_Module_OpenDoc_StdProps_defined
  52. #include <StdProps.xh>
  53. #endif
  54.  
  55. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  56. #include "StdTypes.xh"
  57. #endif
  58.  
  59. #ifndef SOM_ODTranslation_xh
  60. #include <Translt.xh>
  61. #endif
  62.  
  63. #ifndef _BARRAY_
  64. #include <BArray.h>
  65. #endif
  66.  
  67. #ifndef _STDTYPIO_
  68. #include <StdTypIO.h>
  69. #endif
  70.  
  71. #ifndef _PLFMFILE_
  72. #include <PlfmFile.h>
  73. #endif
  74.  
  75. #ifndef _EXCEPT_
  76. #include <Except.h>
  77. #endif
  78.  
  79. #ifndef _ODMEMORY_
  80. #include <ODMemory.h>
  81. #endif
  82.  
  83. #ifndef __TEXTEDIT__
  84. #include <TextEdit.h>
  85. #endif
  86.  
  87. #ifndef _TEMPOBJ_
  88. #include <TempObj.h>
  89. #endif
  90.  
  91. //==============================================================================
  92. // ODSUAddPropValue
  93. //==============================================================================
  94.  
  95. void        ODSUAddPropValue(Environment* ev,
  96.                             ODStorageUnit* su, ODPropertyName prop, ODValueType val)
  97. {
  98.     su->AddProperty(ev, prop)->AddValue(ev, val);
  99. }
  100.  
  101. //==============================================================================
  102. // ODSUForceFocus
  103. //==============================================================================
  104.  
  105. void        ODSUForceFocus(Environment* ev, 
  106.                             ODStorageUnit* su, ODPropertyName prop, ODValueType val)
  107. {
  108.     if (prop != kODNULL) {
  109.         if (su->Exists(ev, prop, kODNULL, 0) == kODFalse)
  110.             su->AddProperty(ev, prop);
  111.         else
  112.             su->Focus(ev, prop, kODPosUndefined, kODNULL, 0, kODPosUndefined);
  113.     }
  114.     if (val != kODNULL) { 
  115.         if (su->Exists(ev, prop, val, 0) == kODFalse)
  116.             su->AddValue(ev, val);
  117.         else
  118.             su->Focus(ev, prop, kODPosSame, val, 0, kODPosUndefined);
  119.     }
  120.     // else; // Presumably the caller has already focussed the su somewhere,
  121.              // perhaps midstream. -TC
  122. }
  123.  
  124. //==============================================================================
  125. // ODSUExistsThenFocus
  126. //==============================================================================
  127.  
  128. ODBoolean        ODSUExistsThenFocus(Environment* ev, 
  129.                             ODStorageUnit* su, ODPropertyName prop, ODValueType val)
  130. {
  131.     if (prop == kODNULL && val == kODNULL)
  132.         return kODTrue;
  133.     // else; // Presumably the caller has already focussed the su somewhere,
  134.              // perhaps midstream. Property:kODNULL && ValueType:kODNULL always 'exist'. -TC
  135.              
  136.     if (su->Exists(ev, prop, val, 0))
  137.     {
  138.         if ( val )
  139.             su->Focus(ev,  prop, kODPosSame, val, 0, kODPosSame);
  140.         else
  141.             su->Focus(ev,  prop, kODPosSame, kODNULL, 0, kODPosAll);
  142.         return kODTrue;    
  143.     }
  144.     else
  145.     {
  146.         return kODFalse;
  147.     }
  148. }
  149.  
  150. //==============================================================================
  151. // ODSURemoveProperty
  152. //==============================================================================
  153.  
  154. void ODSURemoveProperty(Environment* ev, ODStorageUnit* su, ODPropertyName prop)
  155. {
  156.     if ( ODSUExistsThenFocus(ev, su, prop, kODNULL) )
  157.         su->Remove(ev);
  158. }
  159.  
  160. //----------------------------------------------------------------------------------------
  161. // GetPlatformFileFromContainer
  162. //----------------------------------------------------------------------------------------
  163. PlatformFile*    GetPlatformFileFromContainer(Environment* ev, ODContainer* container)
  164. {
  165.     ODByteArray    ba = container->GetID(ev);
  166.     PlatformFile*    file = new PlatformFile();
  167.     file->Specify((ODFileSpec*) ba._buffer);
  168.     ODDisposePtr(ba._buffer);
  169.     
  170.     return file;
  171. }
  172.  
  173. //----------------------------------------------------------------------------------------
  174. // GetODFileSpecFromContainer
  175. //----------------------------------------------------------------------------------------
  176. ODFileSpec GetODFileSpecFromContainer(Environment* ev, ODContainer* container)
  177. {
  178.     ODByteArray    ba = container->GetID(ev);
  179.     ODFileSpec documentSpec = *((ODFileSpec*) ba._buffer);
  180.     ODDisposePtr(ba._buffer); // DMc dispose when done
  181.  
  182.     return documentSpec;
  183. }
  184.  
  185. //----------------------------------------------------------------------------------------
  186. // CreateFileContainer
  187. //----------------------------------------------------------------------------------------
  188. ODContainer*    CreateFileContainer(Environment* ev, ODSession* session, ODFileSpec* fsSpec)
  189. {
  190.     ODByteArray*    ba = CreateByteArray(fsSpec, sizeof(short) + sizeof(long) + fsSpec->name[0] + 1);
  191.     ODContainer*    newContainer = session->GetStorageSystem(ev)->
  192.         CreateContainer(ev,kODDefaultFileContainer /*file->GetContainerType()*/,ba);
  193.     DisposeByteArray(ba);
  194.     
  195.     return newContainer;
  196. }
  197.  
  198. //----------------------------------------------------------------------------------------
  199. // GetFileContainer
  200. //----------------------------------------------------------------------------------------
  201. ODContainer*    GetFileContainer(Environment* ev, ODSession* session, ODFileSpec* fsSpec)
  202. {
  203.     ODByteArray*    ba = CreateByteArray(fsSpec, sizeof(short) + sizeof(long) + fsSpec->name[0] + 1);
  204.     ODContainer*    newContainer = session->GetStorageSystem(ev)->
  205.         AcquireContainer(ev,kODDefaultFileContainer /*file->GetContainerType()*/,ba);
  206.     DisposeByteArray(ba);
  207.     
  208.     return newContainer;
  209. }
  210.     
  211. //----------------------------------------------------------------------------------------
  212. // CreateMemoryContainer
  213. //----------------------------------------------------------------------------------------
  214. ODContainer* CreateMemoryContainer(Environment* ev,
  215.                 ODSession* session,
  216.                 ODHandle handle,
  217.                 ODContainerType containerType)
  218. {
  219.     ODByteArray* ba;
  220.     TRY
  221.         ODLockHandle(handle);
  222.         ba = CreateByteArray(&handle, sizeof(ODHandle));
  223.         ODUnlockHandle(handle);
  224.     CATCH_ALL
  225.         ODUnlockHandle(handle);
  226.         RERAISE;
  227.     ENDTRY
  228.  
  229.     ODContainer* newContainer = session->GetStorageSystem(ev)->
  230.         CreateContainer(ev, containerType, ba);
  231.     DisposeByteArray(ba);
  232.     
  233.     return newContainer;
  234. }
  235.  
  236. //----------------------------------------------------------------------------------------
  237. // GetMemoryContainer
  238. //----------------------------------------------------------------------------------------
  239. ODContainer* GetMemoryContainer(Environment* ev, 
  240.                 ODSession* session,
  241.                 ODHandle handle,
  242.                 ODContainerType containerType)
  243. {
  244.     ODByteArray* ba;
  245.     TRY
  246.         ODLockHandle(handle);
  247.         ba = CreateByteArray(&handle, sizeof(ODHandle));
  248.         ODUnlockHandle(handle);
  249.     CATCH_ALL
  250.         ODUnlockHandle(handle);
  251.         RERAISE;
  252.     ENDTRY
  253.  
  254.     ODContainer* newContainer = session->GetStorageSystem(ev)->
  255.         AcquireContainer(ev, containerType, ba);
  256.     DisposeByteArray(ba);
  257.     
  258.     return newContainer;
  259. }
  260.  
  261. //----------------------------------------------------------------------------------------
  262. // StorageUnitGetValue
  263. //----------------------------------------------------------------------------------------
  264. ODULong    StorageUnitGetValue(ODStorageUnit* su,
  265.                             Environment* ev,
  266.                             ODULong    size,
  267.                             ODPtr buffer)
  268. {
  269.     ODByteArray    ba;    
  270.     ODULong bytesRead = su->GetValue(ev, size, &ba);
  271.     ODBlockMove(ba._buffer, buffer, bytesRead);
  272.     ODDisposePtr(ba._buffer);
  273.  
  274.     return bytesRead;
  275. }
  276.  
  277. //----------------------------------------------------------------------------------------
  278. // StorageUnitViewGetValue
  279. //----------------------------------------------------------------------------------------
  280. ODULong    StorageUnitViewGetValue(ODStorageUnitView* suv,
  281.                             Environment* ev,
  282.                             ODULong    size,
  283.                             ODPtr buffer)
  284. {
  285.     ODByteArray    ba;
  286.     
  287.     ODULong bytesRead = suv->GetValue(ev, size, &ba);
  288.     ODBlockMove(ba._buffer, buffer, ba._length);
  289.     ODDisposePtr(ba._buffer);
  290.     
  291.     return bytesRead;
  292. }
  293.  
  294.  
  295. //----------------------------------------------------------------------------------------
  296. // StorageUnitSetValue
  297. //----------------------------------------------------------------------------------------
  298. void    StorageUnitSetValue(ODStorageUnit* su,
  299.                             Environment* ev,
  300.                             ODULong    size,
  301.                             const void *buffer)
  302. {
  303.     ODByteArray ba;
  304.     ba._length = size;
  305.     ba._maximum = size;
  306.     ba._buffer = (octet*) buffer;
  307.     su->SetValue(ev, &ba);
  308. }
  309.  
  310. //----------------------------------------------------------------------------------------
  311. // StorageUnitViewSetValue
  312. //----------------------------------------------------------------------------------------
  313. void    StorageUnitViewSetValue(ODStorageUnitView* suv,
  314.                                 Environment* ev,
  315.                                 ODULong    size,
  316.                                 const void *buffer)
  317. {
  318.     ODByteArray ba;
  319.     ba._length = size;
  320.     ba._maximum = size;
  321.     ba._buffer = (octet*) buffer;
  322.     suv->SetValue(ev, &ba);
  323. }
  324.  
  325. //----------------------------------------------------------------------------------------
  326. // StorageUnitSetPromiseValue
  327. //----------------------------------------------------------------------------------------
  328. void    StorageUnitSetPromiseValue(ODStorageUnit* su,
  329.                                     Environment* ev,
  330.                                     ODValueType valueType,
  331.                                     ODULong offset,
  332.                                     ODULong    size,
  333.                                     const void *buffer,
  334.                                     ODPart *sourcePart)
  335. {
  336.     ODByteArray ba;
  337.     ba._length = size;
  338.     ba._maximum = size;
  339.     ba._buffer = (octet*) buffer;
  340.     su->SetPromiseValue(ev, valueType, offset, &ba, sourcePart);
  341. }
  342.  
  343. //----------------------------------------------------------------------------------------
  344. // StorageUnitGetStylFromStyledText
  345. //----------------------------------------------------------------------------------------
  346. ODBoolean StorageUnitGetStylFromStyledText(ODStorageUnit* su,
  347.                             Environment* ev,
  348.                             ODULong* size,
  349.                             ODPtr* styl)
  350. {
  351.     ODBoolean    result = kODFalse;
  352.     ODULong        stxtSize;
  353.     ODULong        stylSize;
  354.     ODUShort    scrpNStyles;
  355.     ODType        applestxt = kODNULL;
  356.  
  357.     const ODPlatformType kODScrapType_stxt = 0x73747874;    // 'stxt'
  358.  
  359.     ODVolatile(applestxt);
  360.     ODVolatile(result);
  361.     
  362.     SOM_TRY
  363.  
  364.         ODTranslation* translation = su->GetSession(ev)->GetTranslation(ev);
  365.         applestxt = translation->GetISOTypeFromPlatformType(ev, kODScrapType_stxt, kODPlatformDataType);
  366.             
  367.         if (ODSUExistsThenFocus(ev, su, kODPropContents, applestxt) )
  368.         {
  369.             stxtSize = su->GetSize(ev);
  370.             if ( stxtSize >= sizeof(ODUShort) )
  371.             {
  372.                 StorageUnitGetValue(su, ev, sizeof(ODUShort), &scrpNStyles);
  373.                 stylSize = (scrpNStyles * sizeof(ScrpSTElement)) + sizeof(ODUShort);
  374.                 if ( stxtSize >= stylSize )
  375.                 {
  376.                     *styl = ODNewPtr(stylSize);
  377.                     *size = stylSize;
  378.                     su->SetOffset(ev, 0);
  379.                     StorageUnitGetValue(su, ev, stylSize, *styl);
  380.                     result = kODTrue;
  381.                 }
  382.             }
  383.         }
  384.         
  385.         delete applestxt;
  386.     
  387.     SOM_CATCH_ALL
  388.             if ( applestxt )
  389.                 delete applestxt;
  390.             result = kODFalse;
  391.     SOM_ENDTRY
  392.  
  393.     return result;
  394. }
  395.  
  396. //------------------------------------------------------------------------------
  397. // GetOriginalCloneKind
  398. //------------------------------------------------------------------------------
  399.  
  400. ODCloneKind GetOriginalCloneKind(Environment* ev, ODDraft* draft)
  401. {
  402.     // If content was put on the clipboard without cloning, there will be no
  403.     // kODPropOriginalCloneKind property in the draft's preferences storage
  404.     // unit.  Assume the original operation was a copy in this case. Since
  405.     // no links cannot be placed on the clipboard directly, there won't be
  406.     // links needing fixup on a paste.
  407.  
  408.     TempODStorageUnit draftProperties = draft->AcquireDraftProperties(ev);
  409.     ODCloneKind cloneKind = ODGetULongProp(ev, draftProperties, kODPropOriginalCloneKind, kODULong);
  410.     
  411.     if ((ODULong)(cloneKind) != 0)
  412.         return cloneKind;
  413.     else
  414.         return kODCloneCopy;
  415. }
  416.  
  417. //------------------------------------------------------------------------------
  418. // RemoveDataInterchangeProperties
  419. //------------------------------------------------------------------------------
  420.  
  421. void RemoveDataInterchangeProperties (Environment* ev,
  422.         ODStorageUnit*    su,
  423.         ODBoolean        clonedToFileContainer)
  424. {
  425.     ODSURemoveProperty(ev, su, kODPropLinkSpec);
  426.     ODSURemoveProperty(ev, su, kODPropMouseDownOffset);
  427.     ODSURemoveProperty(ev, su, kODPropCloneKindUsed);
  428.     if ( !clonedToFileContainer )
  429.     {
  430.         ODSURemoveProperty(ev, su, kODPropProxyContents);
  431.         ODSURemoveProperty(ev, su, kODPropContentFrame);
  432.         ODSURemoveProperty(ev, su, kODPropSuggestedFrameShape);
  433.     }
  434. }
  435.  
  436.